package com.gjintel.framework.log.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.gjintel.framework.log.dao.ILogInfoDao;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.util.StringUtil;

@Repository("logInfoDao")
public class LogInfoDaoImpl extends HibernateGenericDAO implements ILogInfoDao {

	@SuppressWarnings("unchecked")
	@Override
	public void findAll(Page page, LogInfo log, Sort sort, Date startTime, Date endTime) {
		String hql = "from LogInfo li where 1=1 ";
		List params = new ArrayList(2);
		if (log != null) {
			if (!StringUtil.isEmpty(log.getOper())) {
				hql += " and li.oper=? ";
				params.add(log.getOper().trim());
			}
			if (!StringUtil.isEmpty(log.getOpType()) && !"-1".equals(log.getOpType())) {
				hql += " and li.opType=? ";
				params.add(log.getOpType().trim());
			}
			if (!StringUtil.isEmpty(log.getLogContent())) {
				hql += " and li.logContent like ? ";
				params.add("%" + log.getLogContent().trim() + "%");
			}
			if (!StringUtil.isEmpty(log.getOpEntity())) {
				hql += " and li.opEntity=? ";
				params.add(log.getOpEntity().trim());
			}
			if (!StringUtil.isEmpty(log.getOpId())) {
				hql += " and li.opId like ? ";
				params.add("%" + log.getOpId().trim() + "%");
			}
		}
		if (startTime != null) {
			hql += " and li.opTime>=? ";
			params.add(startTime);
		}
		if (endTime != null) {
			hql += " and li.opTime<=? ";
			params.add(endTime);
		}
		this.findAsPage(hql, page, sort, "li", false, params.toArray());
	}

	@Override
	public LogInfo getLogInfoById(String id) {
		String hql ="from LogInfo l where l.id=?";
		List<Object> params = new ArrayList<Object>();
		params.add(id);
		return (LogInfo)this.findUnique(hql, params.toArray());
	}

}
